<?php
/**
 * Automatic cooke sing in filter class
 *
 */
class myRememberFilter extends sfFilter
{
  
  /** 
   * Executes cookie sign in
   *
   */
  public function execute($filterChain)
  {
    // execute this filter only once
    if ($this->isFirstCall())
    {
      // execute only if user is not authenticated
      if ( !$this->getContext()->getUser()->isAuthenticated() )
      {   
      	//TODO: set cookie name dynamically
        $cookie_name = sfConfig::get('app_cookie_name');           
        if ($user_cookie = $this->getContext()->getRequest()->getCookie($cookie_name)) 
        {
          list($key, $username) = explode('-', $user_cookie); 
	  
          $c = new Criteria();
          $c->add(UserPeer::REMEMBER, $key);
          $c->add(UserPeer::USERNAME, $username);
          $user = UserPeer::doSelectOne($c); 

          if ( $user )
          {         
            $this->getContext()->getUser()->signIn($user, false);	    
          }
        }
      }
    } 
    // execute next filter
    $filterChain->execute();
  }

}
